# coding: utf-8

import os
import sys
import json
import webbrowser
from read_data import read_data



def to_json(sequence):
    data = {}
    for k in sorted(sequences):
        data[k] = []
        for f in sorted(sequences[k]):
            data[k].append({
                'prop': f.prop,
                'start': f.start,
                'end': f.end,
            })
    return json.dumps(data)



def write_html(name, data,
               html_template='web/fluents.template',
               filename='web/fluents.html'):
    html = open(html_template).read() % {
        'name': name,
        'data': data,
    }
    
    f = open(filename, 'w')
    f.write(html)
    f.close()
    
    return os.path.abspath(f.name)



if __name__ == '__main__':
    filename = sys.argv[1]
    sequences = read_data(filename)
    name = os.path.splitext(os.path.basename(filename))[0]
    data = to_json(sequences)
    htmlfile = write_html(name, data)
    webbrowser.open('file://' + htmlfile)
